﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Models;
using BLL;
using Delete;
using MySql.Data.MySqlClient;


namespace LinSM
{
    /// <summary>
    /// 角色信息
    /// </summary>
    public partial class CharInfo : Form
    {
        public CharInfo()
        {
            InitializeComponent();
        }

        public CharInfo(Characters characters, DataGridView dataGridView)
        {
            InitializeComponent();
            cha = characters;
            dgv = dataGridView;
        }
        Characters cha;
        DataGridView dgv;
        CharInfoManager cim = new CharInfoManager();
        DeleteChar dc = new DeleteChar();
        CharManager cm = new CharManager();
        Models.Characters mchar = new Models.Characters();
        string id = string.Empty;
        int itemType=1;
        string name = string.Empty;
        string clanID = string.Empty;
        private void CharInfo_Load(object sender, EventArgs e)
        {
            id = dgv.CurrentRow.Cells[0].Value.ToString();//dgv活动行属性
            //Location = new Point(cha.Location.X + 50, cha.Location.Y + 150);
            mchar = cim.GetCharInfo(id);
            GetCharInfo();
            dgvItem.DataSource = cim.GetAllItem(id, itemType);//背包物品
            dgvItem.AutoGenerateColumns = false;//取消dgv自动增加列
            if (mchar.Type == 2)
            {
                btnItem3.Enabled = true;
            }
            if (mchar.ClanID > 0)
            {
                btnItem4.Enabled = true;
            }
            name = mchar.Char_name;
            if (Online.isOnline(mchar.Account_name))
            {
                Text = Text + "-[" + mchar.Account_name + "]-" + "[在线]";
            }
            else
            {
                Text = Text + "-[" + mchar.Account_name + "]-" + "[不在线]";
            }

        }

        private void CharInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            cha.have = null;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            
            int str = Convert.ToInt32(txtStr.Text.Trim());
            int con = Convert.ToInt32(txtCon.Text.Trim());
            int dex = Convert.ToInt32(txtDex.Text.Trim());
            int cha = Convert.ToInt32(txtCha.Text.Trim());
            int intel = Convert.ToInt32(txtIntel.Text.Trim());
            int wis = Convert.ToInt32(txtWis.Text.Trim());
            int sex = rdo0.Checked ? 0 : 1;

            if (cim.SetCharInfo(str, con, dex, cha, intel, wis, sex, name))
            {
                picChar.Image = Image.FromFile("img/" + mchar.Type.ToString() + sex.ToString() + ".jpg");
                dgv.CurrentRow.Cells[5].Value = rdo0.Checked ? '男' : '女';//修改后的信息返回dgv
                MessageBox.Show("修改成功");
            }
        }

        private void GetCharInfo()
        {
            //获取角色信息
            double curHp = mchar.CurHp;
            double maxHp = mchar.MaxHp;
            if (curHp > maxHp)
            {
                curHp = maxHp;
            }
            double newHp = (curHp / maxHp) * 100;
            lblMaxHp.Width = 100;
            lblCurHp.Width = Convert.ToInt32(newHp);
            double curMp = mchar.CurMp;
            double maxMp = mchar.MaxMp;
            if (curMp > maxMp)
            {
                curMp = maxMp;
            }
            double newMp = (curMp / maxMp) * 100;
            lblMaxMp.Width = 100;
            lblCurMp.Width = Convert.ToInt32(newMp);
            lblName.Text = mchar.Char_name;
            if (mchar.Lawful < 0)
            {
                lblName.ForeColor = Color.Red;
            }
            txtCha.Text = mchar.Cha.ToString();
            txtCon.Text = mchar.Con.ToString();
            txtDex.Text = mchar.Dex.ToString();
            txtIntel.Text = mchar.Intel.ToString();
            txtStr.Text = mchar.Str.ToString();
            txtWis.Text = mchar.Wis.ToString();
            int sex = mchar.Sex;
            if (sex == 0)
            {
                rdo0.Checked = true;

            }
            else
            {
                rdo1.Checked = true;
            }
            lblMap.Text = cim.GetMapNameBYMapId(mchar.Mapid);
            picChar.Image = Image.FromFile("img/"+mchar.Type.ToString()+sex.ToString()+".jpg");
        }

        private void btnItem1_Click(object sender, EventArgs e)
        {
            itemType = 1;
            dgvItem.DataSource = cim.GetAllItem(id, itemType);
        }

        private void btnItem2_Click(object sender, EventArgs e)
        {
            itemType = 2;
            dgvItem.DataSource = cim.GetAllItem(id, itemType);
        }

        private void btnItem3_Click(object sender, EventArgs e)
        {
            itemType = 3;
            dgvItem.DataSource = cim.GetAllItem(id, itemType);
        }

        private void btnItem4_Click(object sender, EventArgs e)
        {
            clanID = mchar.ClanName;
            itemType = 4;
            dgvItem.DataSource = cim.GetAllItem(clanID, itemType);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvItem.SelectedRows.Count == 0)
            {
                return;
            }
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            string itemId = dgvItem.CurrentRow.Cells[1].Value.ToString();
            if (cim.DeleteItem(itemId, itemType))
            {
                MessageBox.Show("删除成功");
                if (itemType == 4)
                {
                    dgvItem.DataSource = cim.GetAllItem(clanID, itemType);
                }
                else 
                {
                    dgvItem.DataSource = cim.GetAllItem(id, itemType);
                }
            }
        }

        private void btnAllDelete_Click(object sender, EventArgs e)
        {
            if (dgvItem.SelectedRows.Count == 0)
            {
                return;
            }
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            string AccId = string.Empty;
            switch (itemType)
            {
                case 1:
                    AccId = dgvItem.CurrentRow.Cells[4].Value.ToString();
                    break;
                case 2:
                    AccId = dgvItem.CurrentRow.Cells[0].Value.ToString();
                    break;
                case 3:
                    AccId = dgvItem.CurrentRow.Cells[4].Value.ToString();
                    break;
            }
            if (cim.AllDeleteItem(AccId, itemType))
            {
                MessageBox.Show("删除全部成功");
                dgvItem.DataSource = cim.GetAllItem(id, itemType);
            }
        }
        
        private void btn1_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (cim.CharAux(name, 1))
            {
                MessageBox.Show("设置成功");
            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (cim.CharAux(name, 2))
            {
                MessageBox.Show("设置成功");
            }
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (cim.CharAux(name, 5))
            {
                //MessageBox.Show(dgv.CurrentRow.Cells[7].Value.ToString());
                dgv.CurrentRow.Cells[7].Value = Convert.ToInt32(dgv.CurrentRow.Cells[7].Value) + 100;//修改后的信息返回dgv
                dgv.CurrentRow.Cells[8].Value = Convert.ToInt32(dgv.CurrentRow.Cells[8].Value) + 100;//修改后的信息返回dgv
                MessageBox.Show("设置成功");
            }
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (cim.CharAux(name, 3))
            {
                MessageBox.Show("设置成功");
            }
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (cim.CharAux(name, 4))
            {
                MessageBox.Show("设置成功");
            }
        }

        private void btn11_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (cim.CharAux(name, 6))
            {
                dgv.CurrentRow.Cells[6].Value = Convert.ToInt32(dgv.CurrentRow.Cells[6].Value) -10;//修改后的信息返回dgv
                MessageBox.Show("设置成功");
            }
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (cim.CharAux(name, 7))
            {
                MessageBox.Show("设置成功");
            }
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (cim.CharAux(name, 8))
            {
                MessageBox.Show("设置成功");
            }
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (cim.CharAux(name, 9))
            {
                MessageBox.Show("设置成功");
            }
        }

        private void btn10_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (cim.CharAux(name, 10))
            {
                dgv.CurrentRow.Cells[3].Value = 99;//修改后的信息返回dgv
                MessageBox.Show("设置成功");
            }
        }

        private void lblCurHp_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            //补满HP
            if (cim.CharAux(name, 11))
            {
                lblCurHp.Width = 100;
                MessageBox.Show("设置成功");
            }
        }

        private void lblCurMp_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            //补满MP
            if (cim.CharAux(name, 12))
            {
                lblCurMp.Width = 100;
                MessageBox.Show("设置成功");
            }
        }

        private void btnALL_Click(object sender, EventArgs e)
        {
            AllItem ai = new AllItem();
            ai.ShowInTaskbar = false;
            ai.ShowDialog();
        }

        private void btnDeleteChar_Click(object sender, EventArgs e)
        {
            if (Online.isOnline(mchar.Account_name))
            {
                MessageBox.Show("玩家处于在线状态，该操作不支持！");
                return;
            }
            if (mchar.Type == 0)//如果角色是王族
            {
                dc.clan_data(id);//删除血盟
                cim.AllDeleteItem(mchar.ClanName, 4);//删除盟仓
                dc.updateClan(mchar.ClanID.ToString());//解散血盟
            }
            if (mchar.Type == 2)//如果角色是妖精
            {
                cim.AllDeleteItem(id, 3);//删除妖仓
            }
            if (dc.accCount(mchar.Account_name) == 1)//如果角色只有一个
            {
                cim.AllDeleteItem(mchar.Account_name, 2);//删除仓库
                dc.accounts(mchar.Account_name);//删除账号
            }
            if (dc.characters(id))//删除角色
            {
                dc.character_Buddys(id);//删除好友
                dc.character_buff(id);//删除状态
                dc.character_config(id);//删除配置
                dc.character_quests(id);//删除任务
                dc.character_skills(id);//删除技能
                dc.character_teleport(id);//删除坐标
                cim.AllDeleteItem(id, 1);//删除背包
                MessageBox.Show("角色删除成功！");
                CharInfo.ActiveForm.Close();//关闭角色信息
                dgv.DataSource = cm.GetAllChar();//重新加载角色列表
            }
        }
    }
}
